﻿@page "/Wms/BuyInWarehouse/Index"
@using nERP.ViewModel.Wms.BuyInWarehouseVMs
@using System.ComponentModel.DataAnnotations
@inherits BasePage
@attribute [ActionDescription("_Page.Wms.BuyInWarehouse.Index", "nERP.Wms.Controllers,BuyInWarehouse")]

<Table @ref="BuyInWarehouseListVMdataTable" TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View" OnQueryAsync="BuyInWarehouseListVMOnSearchBuyInWarehouse"
       IsStriped="true" IsBordered="true" ShowRefresh="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
       ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
       AllowResizing="true" ShowDefaultButtons="false" IsPagination="true" AllowDragColumn="true" ShowColumnList="true" ShowSearch="true" SearchTooltip=""
       OnResetSearchAsync="OnResetSearchAsync" SearchMode="SearchMode.Top" CollapsedTopSearch="true" ShowSearchText="false">
    <SearchTemplate>
        <ValidateForm Model="@Model">
            <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
                <BootstrapInput @bind-Value="@Model.Searcher.BuyInWarehouseCode" />
                <Select @bind-Value="@Model.Searcher.BuyId" Items="AllBuys" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <Select @bind-Value="@Model.Searcher.SupplierId" Items="AllSuppliers" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <WTDateRange @bind-Value="@Model.Searcher.InvoicesDate" />
                <Select @bind-Value="@Model.Searcher.Status" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
                <WTDateRange @bind-Value="@Model.Searcher.CreateTime" />
                <BootstrapInput @bind-Value="@Model.Searcher.CreateBy" />
            </Row>
        </ValidateForm>
    </SearchTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.BuyInWarehouse_BuyInWarehouseCode" Text="@WtmBlazor.Localizer["Page.入库单号"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_Buy" Text="@WtmBlazor.Localizer["Page.采购订单"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_Supplier" Text="@WtmBlazor.Localizer["Page.供应商"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_InvoicesDate" Text="@WtmBlazor.Localizer["Page.单据日期"]" FormatString="yyyy-MM-dd" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_Status" Text="@WtmBlazor.Localizer["Page.单据状态"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
        <TableColumn @bind-Field="@context.BuyInWarehouse_IsValid" Text="@WtmBlazor.Localizer["_Admin.IsValid"]" />
    </TableColumns>

    <RowButtonTemplate>
        @if (context.wtmtemp_CanApprove == true)
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="-o" Color="Color.Warning" Text="@WtmBlazor.Localizer["Page.审批"]" OnClick="()=>OnApproveClick(context)" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/Edit") && context.BuyInWarehouse_Status != nERP.Model.StatusTypeEnum.Reviewed)  //已审批不可见
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-pencil-square" Color="Color.Warning" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="()=>OnEditClick(context)" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/{id}"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-info-circle" Color="Color.Info" Text="@WtmBlazor.Localizer["Page.详情"]" OnClick="()=>OnDetailsClick(context)" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/BatchDelete") && context.BuyInWarehouse_Status != nERP.Model.StatusTypeEnum.Reviewed)  //已审批不可见
        {
            <PopConfirmButton OnConfirm="() => OnDeleteClick(context)" Size="Size.ExtraSmall" Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]"
                              ConfirmButtonColor="Color.Danger" />
        }
    </RowButtonTemplate>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/Wms/BuyInWarehouse/Create"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View" Icon="fa fa-plus" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="(x)=>OnCreateClick()" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/BatchDelete"))
        {
            <TableToolbarPopConfirmButton TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View"
                                          Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/BatchEdit"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.BatchEdit"]" OnClickCallback="(x)=>OnBatchEditClick()" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/Import"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View" Icon="fa fa-tasks" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="(x)=>OnImportClick()" />
        }
        @if (IsAccessable("/api/Wms/BuyInWarehouse/BuyInWarehouseExportExcel"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View" Icon="fa fa-arrow-circle-down" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="(x)=>OnExportClick()" IsAsync="true" />
        }
    </TableToolbarTemplate>

</Table>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private BuyInWarehouseListVM Model = new BuyInWarehouseListVM();
    private Table<nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View> BuyInWarehouseListVMdataTable;
    private Task OnResetSearchAsync(BuyInWarehouse_View item)
    {
        Model.Searcher = new BuyInWarehouseSearcher();
        item = new BuyInWarehouse_View();
        return Task.CompletedTask;
    }
    private async Task BuyInWarehouseListVMDoSearch()
    {
        await BuyInWarehouseListVMdataTable.QueryAsync();
    }
    private async Task<QueryData<nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View>> BuyInWarehouseListVMOnSearchBuyInWarehouse(QueryPageOptions opts)
    {

        return await StartSearch<nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View>("/api/Wms/BuyInWarehouse/SearchBuyInWarehouse", Model.Searcher, opts);
    }
    private async Task OnApproveClick(nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View item)
    {
        if (await OpenDialog<Approve>(@WtmBlazor.Localizer["Page.审批"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
    }

    private async Task OnCreateClick()
    {
        var id = BuyInWarehouseListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Create>(@WtmBlazor.Localizer["Sys.Create"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View item)
    {
        if (await OpenDialog<Edit>(@WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View item)
    {
        if (await OpenDialog<Details>(@WtmBlazor.Localizer["Page.详情"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDeleteClick(nERP.ViewModel.Wms.BuyInWarehouseVMs.BuyInWarehouse_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/Wms/BuyInWarehouse/batchdelete", (s) => "Sys.OprationSuccess");
        await BuyInWarehouseListVMdataTable.QueryAsync();
    }

    private async Task OnBatchDeleteClick()
    {
        if (BuyInWarehouseListVMdataTable.SelectedRows?.Any() == true)
        {
            if (BuyInWarehouseListVMdataTable.SelectedRows?.Where(z0 => z0.BuyInWarehouse_Status == nERP.Model.StatusTypeEnum.Reviewed).Count() > 0)
            {
                await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], "选择的项目有【已审批】单据，不可操作！");
                return;
            }
            await PostsData(BuyInWarehouseListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList(), $"/api/Wms/BuyInWarehouse/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnBatchEditClick()
    {
        if (BuyInWarehouseListVMdataTable.SelectedRows?.Any() == true)
        {
            if (BuyInWarehouseListVMdataTable.SelectedRows?.Where(z0 => z0.BuyInWarehouse_Status == nERP.Model.StatusTypeEnum.Reviewed).Count() > 0)
            {
                await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], "选择的项目有【已审批】单据，不可操作！");
                return;
            }
            if (await OpenDialog<BatchEdit>(WtmBlazor.Localizer["Sys.BatchEdit"], x => x.ids == BuyInWarehouseListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToArray(), isMax: false) == DialogResult.Yes)
            {
                await BuyInWarehouseListVMdataTable.QueryAsync();
            }
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnImportClick()
    {
        var id = BuyInWarehouseListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Import>(@WtmBlazor.Localizer["Sys.Import"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await BuyInWarehouseListVMdataTable.QueryAsync();
        }
    }

    private async Task OnExportClick()
    {
        if (BuyInWarehouseListVMdataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/Wms/BuyInWarehouse/BuyInWarehouseExportExcelByIds", BuyInWarehouseListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/Wms/BuyInWarehouse/BuyInWarehouseExportExcel", Model.Searcher);
        }
    }


    private List<SelectedItem> AllBuys = new List<SelectedItem>();
    private List<SelectedItem> AllSuppliers = new List<SelectedItem>();
    protected override async Task OnInitializedAsync()
    {


        AllBuys = await WtmBlazor.Api.CallItemsApi("/api/Wms/BuyInWarehouse/GetBuys");
        AllBuys.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllSuppliers = await WtmBlazor.Api.CallItemsApi("/api/Wms/BuyInWarehouse/GetSuppliers");
        AllSuppliers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });

        await base.OnInitializedAsync();
    }
}
